package org.udr.datamodel;

import java.util.Iterator;

import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.ToOne;
import org.hibernate.mapping.Value;

public class ConfigurationTestHelper {

	public static void printCollection(Collection col) {

		Value elem = col.getElement();
		String name = col.getClass().getName();

		if (elem instanceof OneToMany) {
			System.out.println(name + " has-element OneToMany");
			return;
		}

		if (elem instanceof ManyToOne) {
			System.out.println(name + " has-element ManyToOne");
			return;
		}

		if (elem instanceof Component) {
			System.out.println(name + " has-element Component");
			return;
		}

		if (elem instanceof SimpleValue) {
			System.out.println(name + " has-element SimpleValue");
			return;
		}
	}

	public static void printComponent(Component comp) {
		System.out.println("component " + comp.getComponentClassName());
		printProperties(comp.getPropertyIterator());
	}

	public static void printToOne(org.hibernate.mapping.Property prop, ToOne toOne) {
		boolean cascadeDelete = toOne.isCascadeDeleteEnabled();
		String cardinality = "one";
		if (toOne instanceof ManyToOne) {
			cardinality = "many";
		}
		System.out.println("toOne " + prop.getType().getName() + " "
				+ cardinality);
	}

	public static void printProperties(Iterator myProps) {
		while (myProps.hasNext()) {
			org.hibernate.mapping.Property myProp = (org.hibernate.mapping.Property) myProps
					.next();
			Value val = myProp.getValue();

			// one-to-one/many-to-one
			if (val instanceof ToOne) {
				System.out.println(myProp.getName() + " is-a ToOne "
						+ val.getClass().getName());
				printToOne(myProp, (ToOne) val);
				continue;
			}

			// component
			if (val instanceof Component) {
				System.out.println(myProp.getName() + " is-a Component "
						+ val.getClass().getName());
				printComponent((Component) val);
				continue;
			}

			// collections
			if (val instanceof Collection) {
				System.out.println(myProp.getName() + " is-a Collection "
						+ val.getClass().getName());
				printCollection((Collection) val);
				continue;
			}

			// primitive
			if (val instanceof SimpleValue) {
				System.out.println(myProp.getName() + " is-a primitive "
						+ val.getClass().getName());
				continue;
			}

			// QueryList and other things will be taken care of elsewhere
			continue;
		}
	}

	public static void printConfiguration(Configuration conf) {
		Iterator myClasses = conf.getClassMappings();
		while (myClasses.hasNext()) {
			PersistentClass myClass = (PersistentClass) myClasses.next();
			System.out.println("PersistentClass is " + myClass.getNodeName());
			Iterator myClassSubs = myClass.getDirectSubclasses();
			while (myClassSubs.hasNext()) {
				PersistentClass myClassSub = (PersistentClass) myClassSubs
						.next();
				System.out.println(myClassSub.getClassName()
						+ " inherits-from " + myClass.getClassName());
			}

			printProperties(myClass.getPropertyIterator());
		}
	}

}
